var sequencingFields = "yseqKey,yseqDate,yseqState,yseqSampleCount,yseqReads,yseqProject,yseqContact,yseqBudget,yseqStart,yseqEnd,yseqNotes,yseqReadFormat,yseqRefGenome,yseqSamplePrep,yseqSamples,yseqSampleAlias,yseqInstrument," +
"yseqAdaptorKit,yseqShearMethod,yseqMultiplexDegree,yseqKit,yseqSlider,yseqPrimer,yseqNFSSolid,yseqType,yseqSolidBeadKit,yseqSizeSelection," +
"yseqRNAFraction,yseqRNAStrandSpec,yseqRNABCModules,yseqRNANFS,yseqBamSam,yseqFastqc";

var solidNFSPath = "/proj/dudley3/sequencing/solid";
var rnaNFSPath = "/proj/dudley3/sequencing/rnaseq";

function getSequencingSearchColumnHeaders() {
    return  [
        {
            header: 'Index',
            width: 50,
            sortable: true,
            dataIndex: 'indexNum'
        },
        {
            id:'yseqKey',
            header: 'Run',
            width: 60,
            sortable: true,
            dataIndex: 'name'
        },
        {
            header: 'Created',
            width: 70,
            sortable: true,
            dataIndex: 'dateCreated'
        },
        {
            header: 'State',
            width: 70,
            sortable: true,
            dataIndex: 'state'
        },
        {
            header: 'Degree',
            width: 50,
            sortable: true,
            dataIndex: 'multiplexDegree'
        },
        {
            header: 'Length',
            width: 50,
            sortable: true,
            dataIndex: 'readNum'
        },
        {
            header: 'Project',
            width: 140,
            sortable: true,
            dataIndex: 'project'
        },
        {
            header: 'Contact',
            hidden: true,
            sortable: true,
            dataIndex: 'contactEmail'
        },
        {
            header: 'Budget',
            hidden: true,
            dataIndex: 'budget'
        },
        {
            header: 'AlignmentStart',
            hidden: true,
            dataIndex: 'alignmentStart'
        },
        {
            header: 'AlignmentEnd',
            hidden: true,
            dataIndex: 'alignmentEnd'
        },
        {
            header: 'yseqNotes',
            hidden: true,
            dataIndex: 'yseqNotes'
        },
        {
            header: 'readFormat',
            hidden: true,
            dataIndex: 'readFormat'
        },
        {
            header: 'refGenome',
            hidden: true,
            dataIndex: 'refGenome'
        },
        {
            header: 'samplePrep',
            hidden: true,
            dataIndex: 'samplePrep'
        },
        {
            header: 'Samples',
            width: 180,
            dataIndex: 'samples'
        },
        {
            header: 'samplesAlias',
            hidden: true,
            dataIndex: 'samplesAlias'
        },
        {
            header: 'Inst',
            width: 70,
            dataIndex: 'instrument'
        },
        {
            header: 'Type',
            width: 70,
            dataIndex: 'yseqType'
        },
        {
            header: 'adaptorKit',
            hidden: true,
            dataIndex: 'adaptorKit'
        },
        {
            header: 'shearMethod',
            hidden: true,
            dataIndex: 'shearMethod'
        },
        {
            header: 'multiplexDegree',
            hidden: true,
            dataIndex: 'multiplexDegree'
        },
        {
            header: 'seqKit',
            hidden: true,
            dataIndex: 'seqKit'
        },
        {
            header: 'slider',
            hidden: true,
            dataIndex: 'slider'
        }
    ];
}

function getSequencingPagingColumns() {
    return [
        {
            name: 'indexNum',
            type: 'float'
        },
        {
            name: 'name'
        },
        {
            name: 'dateCreated'
        },
        {
            name: 'state'
        },
        {
            name: 'sampleCount'
        },
        {
            name: 'readNum'
        },
        {
            name: 'project'
        },
        {
            name: 'contactEmail'
        },
        {
            name: 'budget'
        },
        {
            name: 'alignmentStart'
        },
        {
            name: 'alignmentEnd'
        },
        {
            name: 'yseqNotes'
        },
        {
            name: 'readFormat'
        },
        {
            name: 'refGenome'
        },
        {
            name: 'samplePrep'
        },
        {
            name: 'samples'
        },
        {
            name: 'samplesAlias'
        },
        {
            name: 'instrument'
        },
        {
            name: 'adaptorKit'
        },
        {
            name: 'shearMethod'
        },
        {
            name: 'multiplexDegree'
        },
        {
            name: 'seqKit'
        },
        {
            name: 'slider'
        },
        {
            name: 'primer'
        },
        {
            name: 'nfs_solid'
        },
        {
            name: 'yseqType'
        },
        {
            name: 'yseqSolidBeadKit'
        },
        {
            name: 'yseqSizeSelection'
        },
        {
            name: 'yseqRNAFraction'
        },
        {
            name: 'yseqRNAStrandSpec'
        },
        {
            name: 'yseqRNABCModules'
        },
        {
            name: 'yseqRNANFS'
        },
        {
            name: 'yseqBamSam'
        },
	{
            name: 'yseqFastqc'
        }
    ];
}

function translateSeqSampleResultJsonToArray(jsonSearchArray) {
    var jsonObj = [];
    jsonObj[0] = jsonSearchArray.name;
    jsonObj[1] = jsonSearchArray.sampleStatus;
    jsonObj[2] = jsonSearchArray.sampleStatusDate;
    jsonObj[3] = jsonSearchArray.flowcell;
    jsonObj[4] = jsonSearchArray.lane;
    jsonObj[5] = jsonSearchArray.comment;
    return jsonObj;
}


function initSequencingJson(pHash) {
    pHash.seqName = undefined;
}

function getSequencingSearchOptionsArray() {
    return [
        ['all', 'All'],
        ['yseqKey', 'yseqKey'],
        ['seqName', 'seqName'],
        ['yseqDate', 'yseqDate'],
        ['contactEmail', 'contactEmail'],
        ['boxNumber', 'boxNumber'],
        ['state', 'state'],
        ['dateEntered', 'dateEntered'],
        ['yseqNotes', 'yseqNotes'],
        ['readFormat', 'readFormat'],
        ['alignmentStart', 'alignmentStart'],
        ['alignmentEnd', 'alignmentEnd'],
        ['samplePrep', 'samplePrep'],
        ['samples', 'samples'],
        ['instrument', 'instrument'],
        ['adaptorKit', 'adaptorKit'],
        ['shearMethod', 'shearMethod'],
        ['multiplexDegree', 'multiplexDegree'],
        ['seqKit', 'seqKit'],
        ['slider', 'slider'],
        ['primer', 'primer']
    ];
}


function populateSequencingJson(oHash, g) {
    oHash.name = g.getSelectionModel().getSelected().data.name;
    oHash.yseqDate = g.getSelectionModel().getSelected().data.dateCreated;
    oHash.yseqType = g.getSelectionModel().getSelected().data.type;
    oHash.yseqState = g.getSelectionModel().getSelected().data.state;
    oHash.sampleCount = g.getSelectionModel().getSelected().data.sampleCount;
    oHash.numReads = g.getSelectionModel().getSelected().data.readNum;
    oHash.dudleyProject = g.getSelectionModel().getSelected().data.project;
    oHash.budget = g.getSelectionModel().getSelected().data.budget;
    oHash.alignmentStart = g.getSelectionModel().getSelected().data.alignmentStart;
    oHash.alignmentEnd = g.getSelectionModel().getSelected().data.alignmentEnd;
    oHash.contactEmail = g.getSelectionModel().getSelected().data.contactEmail;
    oHash.yseqNotes = g.getSelectionModel().getSelected().data.yseqNotes;
    oHash.samples = g.getSelectionModel().getSelected().data.samples;
    oHash.readFormat = g.getSelectionModel().getSelected().data.readFormat;
    oHash.refGenome = g.getSelectionModel().getSelected().data.refGenome;
    oHash.samplePrep = g.getSelectionModel().getSelected().data.samplePrep;
    oHash.samples = g.getSelectionModel().getSelected().data.samples;
    oHash.samplesAlias = g.getSelectionModel().getSelected().data.samplesAlias;
    oHash.instrument = g.getSelectionModel().getSelected().data.instrument;
    oHash.adaptorKit = g.getSelectionModel().getSelected().data.adaptorKit;
    oHash.shearMethod = g.getSelectionModel().getSelected().data.shearMethod;
    oHash.multiplexDegree = g.getSelectionModel().getSelected().data.multiplexDegree;
    oHash.seqKit = g.getSelectionModel().getSelected().data.seqKit;
    oHash.slider = g.getSelectionModel().getSelected().data.slider;
    oHash.primer = g.getSelectionModel().getSelected().data.primer;
    oHash.nfs_solid = g.getSelectionModel().getSelected().data.nfs_solid;
    oHash.yseqType = g.getSelectionModel().getSelected().data.yseqType;
    oHash.yseqSolidBeadKit = g.getSelectionModel().getSelected().data.yseqSolidBeadKit;
    oHash.yseqSizeSelection = g.getSelectionModel().getSelected().data.yseqSizeSelection;
    oHash.yseqRNAFraction = g.getSelectionModel().getSelected().data.yseqRNAFraction;
    oHash.yseqRNAStrandSpec = g.getSelectionModel().getSelected().data.yseqRNAStrandSpec;
    oHash.yseqRNABCModules = g.getSelectionModel().getSelected().data.yseqRNABCModules;
    oHash.yseqRNANFS = g.getSelectionModel().getSelected().data.yseqRNANFS;
    oHash.yseqBamSam = g.getSelectionModel().getSelected().data.yseqBamSam;
    oHash.yseqFastqc = g.getSelectionModel().getSelected().data.yseqFastqc;

}


function validateCreateUpdateSequencing() {

    /*if (Ext.getCmp("plasmidNameId").getValue() == null || Ext.getCmp("plasmidNameId").getValue() == "") {
     Ext.Msg.alert('Error', 'Plasmid name is required.');
     return false;
     } else if (Ext.getCmp("markersId").getValue() == null || Ext.getCmp("markersId").getValue() == "") {
     Ext.Msg.alert('Error', 'Plasmid info field is required.');
     return false;
     }else if (Ext.getCmp("yeastSelectionId").getValue() == null || Ext.getCmp("yeastSelectionId").getValue() == "") {
     Ext.Msg.alert('Error', 'Yeast selection field is required.');
     return false;
     }else if (Ext.getCmp("yeastOriginId").getValue() == null || Ext.getCmp("yeastOriginId").getValue() == "") {
     Ext.Msg.alert('Error', 'Yeast origin is required.');
     return false;
     }else if (Ext.getCmp("bacterialSelectionId").getValue() == null || Ext.getCmp("bacterialSelectionId").getValue() == "") {
     Ext.Msg.alert('Error', 'Bacterial Selection field is required.');
     return false;
     }*/

    return true;
}

function popupPNGLayout(imageType) {
    var png = "";
    var title = "";    
    if (imageType.indexOf("RAD4") != -1){
        png = '<img src="images/rad4bp.png"/>';
        title = 'RAD 4-Based Sequence Barcode Layout';
    }else if (imageType.indexOf("VIDA") != -1){
        png = '<img src="images/vida-plate-layout.png"/>';
        title = 'Vida (YCR5) Crossing Plate Layout - YO486 x YO502';
    }else if (imageType.indexOf("MICHELLE") != -1){
        png = '<img src="images/michelle-plate-layout.png" />';
        title = 'Michelle (YCR6) Crossing Plate Layout - YO490 x YO512';
    }
    var pwin = new Ext.Window({
        title: title,
        width: 600,
        height: 500,
        modal: true,
        closeAction: 'hide',
        bodyStyle: 'padding:10px;',
        html: png
    });
    pwin.show();
}

function populateSingleSeqViaSearch(strainHash, rec) {
    strainHash.name = rec[1];
    strainHash.yseqDate = rec[2];
    strainHash.yseqState = rec[3];
    strainHash.sampleCount = rec[4];
    strainHash.numReads = rec[5];
    strainHash.dudleyProject = rec[6];
    strainHash.contactEmail = rec[7];
    strainHash.budget = rec[8];
    strainHash.alignmentStart = rec[9];
    strainHash.alignmentEnd = rec[10];
    strainHash.yseqNotes = rec[11];
    strainHash.readFormat = rec[12];
    strainHash.refGenome = rec[13];
    strainHash.samplePrep = rec[14];
    strainHash.samples = rec[15];
    strainHash.samplesAlias = rec[16];
    strainHash.instrument = rec[17];
    strainHash.adaptorKit = rec[18];
    strainHash.shearMethod = rec[19];
    strainHash.multiplexDegree = rec[20];
    strainHash.seqKit = rec[21];
    strainHash.slider = rec[22];
    strainHash.primer = rec[23];
    strainHash.nfs_solid = rec[24];
}

function getSliderTypeCombo(hashObj) {
    var sliderStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSliderTypeArray()
    });
    return new Ext.form.ComboBox({
        store: sliderStore,
        id: 'yseqSlider',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.slider,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getRNAFractionCombo(hashObj) {
    var sliderStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getRNAFractionArray()
    });
    return new Ext.form.ComboBox({
        store: RNAFractionStore,
        id: 'yseqRNAFraction',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.yseqRNAFraction,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getSeqKitCombo(hashObj) {
    var seqKitStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqKitArray()
    });
    return new Ext.form.ComboBox({
        store: seqKitStore,
        id: 'yseqKit',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.seqKit,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getReadFormatCombo(hashObj) {
    var readFormatStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : [
            ['1', 'Single'],
            ['2', 'Paired end'],
            ['3', 'Mate pair'],
            ['4', 'Multiplex Single'],
            ['5', 'Multiplex Paired end']
        ]
    });
    return new Ext.form.ComboBox({
        store: readFormatStore,
        id: 'yseqReadFormat',
        fieldLabel: 'Read format',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.readFormat,
        forceSelection:true,
        selectOnFocus:true
    });
}
function getInstrumentCombo(hashObj) {
    var instrumentData = [
        ['1', 'Illumina'],
        ['2', 'SOLiD']
    ];
    var instrumentStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : instrumentData
    });
    var ic = new Ext.form.ComboBox({
        store: instrumentStore,
        id: 'yseqInstrument',
        fieldLabel: 'Instrument',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.instrument,
        forceSelection:true,
        selectOnFocus:true
    });
    ic.on("select", function (c, r, i) {
        if (i == 1) {
            Ext.getCmp("solidparameters").expand();
        }
        if (i == 0) {
            Ext.getCmp("solidparameters").collapse();
        }
    });
    return ic;
}

function getSeqTypeCombo(hashObj) {
    var sty = new Ext.form.ComboBox({
        store: seqTypeStore,
        id: 'yseqType',
        fieldLabel: 'YSeqType',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.yseqType,
        forceSelection:true,
        selectOnFocus:true
    });
    sty.on("select", function (c, r, i) {
        if (i == 1) {
            Ext.getCmp("rnaparameters").expand();
        }
        if (i == 0) {
            Ext.getCmp("rnaparameters").collapse();
        }
    });
    return sty;
}

function getSizeSelectionCombo(hashObj) {
    return new Ext.form.ComboBox({
        store: sizeSelectionStore,
        id: 'yseqSizeSelection',
        fieldLabel: 'Size Selection',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.yseqSizeSelection,
        forceSelection:true,
        selectOnFocus:true
    });
}
function getEZBeadCombo(hashObj) {
    return new Ext.form.ComboBox({
        store: ezbeadStore,
        id: 'yseqSolidBeadKit',
        fieldLabel: 'EZ Bead Kit',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.yseqSolidBeadKit,
        forceSelection:true,
        selectOnFocus:true
    });
}
function getPrimerCombo(hashObj) {
    var primerStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqPrimerArray()
    });
    return new Ext.form.ComboBox({
        store: primerStore,
        id: 'yseqPrimer',
        fieldLabel: 'Primer',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.primer,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getAdaptorKitCombo(hashObj) {
    var adaptorKitStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqAdaptorArray()
    });
    return new Ext.form.ComboBox({
        store: adaptorKitStore,
        id: 'yseqAdaptorKit',
        fieldLabel: 'Adaptor kit',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.adaptorKit,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getShearMethodCombo(hashObj) {
    var shearMethodStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqShearMethodArray()
    });
    return new Ext.form.ComboBox({
        store: shearMethodStore,
        id: 'yseqShearMethod',
        fieldLabel: 'Fragmentation',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.shearMethod,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getRefGenomeCombo(hashObj) {
  var refGenomeStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqRegGenomeArray()
    });
    return new Ext.form.ComboBox({
        store: refGenomeStore,
        id: 'yseqRefGenome',
        fieldLabel: 'Ref. genome',
        displayField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.refGenome,
        forceSelection:true,
        selectOnFocus:true
    });
}

var chrhash = {};
chrhash['I'] = 'chr01';
chrhash['II'] = 'chr02';
chrhash['III'] = 'chr03';
chrhash['IV'] = 'chr04';
chrhash['V'] = 'chr05';
chrhash['VI'] = 'chr06';
chrhash['VII'] = 'chr07';
chrhash['VIII'] = 'chr08';
chrhash['IX'] = 'chr09';
chrhash['X'] = 'chr10';
chrhash['XI'] = 'chr11';
chrhash['XII'] = 'chr12';
chrhash['XIII'] = 'chr13';
chrhash['XIV'] = 'chr14';
chrhash['XV'] = 'chr15';
chrhash['XVI'] = 'chr16';
chrhash['MT'] = 'chrmt';
